﻿#region Copyright Statement
//  ------------------------------------------------------------------------------
//  
//    This file is part of The Streambolics Library.
//    Copyright © 2005-2009, Stephan Leclercq & Streambolics
//    All rights reserved.
//  
//    The Streambolics Library is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//  
//    The Streambolics Library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//  
//    You should have received a copy of the GNU General Public License
//    along with Streambolics; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//  
//  ------------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Streambolics.Hp41s
{
    /// <summary>
    ///     A static class containing most of the common instructions of the simulator.
    /// </summary>

    public static class CommonInstructions
    {
        #region Flow Control

        [SimpleInstruction ("RTN")]
        public static readonly Instruction Return = new ContextInstruction ((c) => c.DoReturn (), "RTN");
        public static readonly Instruction End = new ContextInstruction ((c) => c.DoReturn (), "END");

        [SimpleInstruction ("PROMPT")]
        public static readonly Instruction Prompt = new ContextInstruction ((c) => c.DoPrompt (c.Registers.Alpha), "PROMPT");

        [SimpleInstruction ("STOP")]
        public static readonly Instruction Stop = new ContextInstruction ((c) => c.DoPrompt (null), "STOP");

        [SimpleInstruction ("CLD")]
        public static readonly Instruction Cld = new ContextInstruction ((c) => c.Machine.Cld (), "CLD");

        [SimpleInstruction ("NULL")]
        public static readonly Instruction Null = new ContextInstruction ((c) =>
        {
        }, "NULL");

        #endregion

        #region Alpha Register

        public static Instruction SetString (string aValue)
        {
            return new StringInstruction (aValue);
        }

        public static Instruction AppendString (string aValue)
        {
            return new AppendStringInstruction (aValue);
        }

        [SimpleInstruction ("CLA")]
        public static readonly Instruction Cla = new RegistersInstruction ((r) => r.Alpha = String.Empty, "CLA");

        [SimpleInstruction ("ASHF")]
        public static readonly Instruction Ashf = new RegistersInstruction ((r) => r.Alpha = r.Alpha.Substring (6, 18), "ASHF");

        [SimpleInstruction ("AVIEW")]
        public static readonly Instruction Aview = new ContextInstruction ((c) => c.Machine.Display = c.Registers.Alpha, "AVIEW");

        [ReferenceInstruction ("ARCL")]
        public static Instruction Arcl (int aRegister, bool aIndirect)
        {
            return new ArclInstruction (aRegister, aIndirect);
        }

        public static Instruction Arcl (int aRegister)
        {
            return Arcl (aRegister, false);
        }

        #endregion

        #region Operators

        private static double PlusOperator (double x, double y)
        {
            return x + y;
        }

        #endregion

        #region Operand Stack

        [SimpleInstruction ("ENTER")]
        [SimpleInstruction ("ENTER^")]
        [SimpleInstruction ("ENTER↑")]
        public static readonly Instruction Enter = new RegistersInstruction ((r) => r.Enter (), "ENTER↑");

        public static Instruction Number (double aValue)
        {
            return new NumberInstruction (aValue);
        }

        #endregion

        #region Registers

        [ReferenceInstruction ("STO")]
        public static Instruction Sto (int aRegister, bool aIndirect)
        {
            return new StoInstruction (aRegister, aIndirect);
        }

        public static Instruction Sto (int aRegister)
        {
            return Sto (aRegister, false);
        }

        [ReferenceInstruction ("ST+")]
        public static Instruction StoPlus (int aRegister, bool aIndirect)
        {
            return new StoOpInstruction ("ST+", aRegister, aIndirect, PlusOperator);
        }

        #endregion

        #region Arithmetic

        [SimpleInstruction ("+")]
        public static readonly Instruction Plus = new DiadicInstruction (PlusOperator, "+");

        [SimpleInstruction ("-")]
        public static readonly Instruction Minus = new DiadicInstruction ((x, y) => y - x, "-");

        [SimpleInstruction ("*")]
        public static readonly Instruction Times = new DiadicInstruction ((x, y) => x * y, "*");

        [SimpleInstruction ("/")]
        public static readonly Instruction Divide = new DiadicInstruction ((x, y) => y / x, "/");

        #endregion

    }
}
